package com.oyjd.beauty.business.user.action;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.ibm.icu.text.SimpleDateFormat;
import com.oyjd.beauty.bean.vo.UserVO;
import com.oyjd.beauty.business.user.service.UserService;
import com.oyjd.beauty.business.user.vo.UserExVO;
import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.CodeUtil;
import com.oyjd.beauty.fw.util.OutUtil;
import com.oyjd.beauty.fw.util.SecurityUtil;
import com.oyjd.beauty.pub.Const;

/**
 * 
 * 用户管理输入输出Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/user")
@Results({ @Result(name = "viewList", location = Const.JSP_user_list), @Result(name = "viewAdd", location = Const.JSP_user_add), @Result(name = "viewEdit", location = Const.JSP_user_edit), @Result(name = "viewPersonEdit", location = Const.JSP_user_person_edit), @Result(name = "viewPersonResetPwd", location = Const.JSP_user_person_reset_pwd) })
public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7959507078321287802L;
	private static final Log log = LogFactory.getLog(UserAction.class);

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private UserService service;

	@Autowired
	public void setService(UserService service) {
		this.service = service;
	}

	/**
	 * 进入列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String userId = ServletRequestUtils.getStringParameter(req, "id");
			UserVO vo = (UserVO) service.getUser(userId);
			req.setAttribute("user", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 进入修改个人资料页面
	 * 
	 * @return
	 */
	public String viewPersonEdit() {
		try {
			HttpServletRequest req = getRequest();
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			UserVO vo = (UserVO) service.getUser(userVO.getUserId());
			req.setAttribute("user", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewPersonEdit";
	}

	/**
	 * 进入修改个人密码页面
	 * 
	 * @return
	 */
	public String viewPersonResetPwd() {
		return "viewPersonResetPwd";
	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<UserExVO> page = service.getList(new Page<UserExVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增资料
	 * 
	 * @return
	 */
	public String add() {
		try {
			HttpServletRequest req = getRequest();
			String roleId = ServletRequestUtils.getStringParameter(req, "roleId");
			String account = ServletRequestUtils.getStringParameter(req, "account");
			String pwd = ServletRequestUtils.getStringParameter(req, "pwd");
			String name = ServletRequestUtils.getStringParameter(req, "name", "");
			int sex = ServletRequestUtils.getIntParameter(req, "sex", 1);
			String birthday = ServletRequestUtils.getStringParameter(req, "birthday", "");
			String phone = ServletRequestUtils.getStringParameter(req, "phone", "");
			String email = ServletRequestUtils.getStringParameter(req, "email", "");
			String idcard = ServletRequestUtils.getStringParameter(req, "idcard", "");
			float height=ServletRequestUtils.getFloatParameter(req, "height", 0f);
			float weight=ServletRequestUtils.getFloatParameter(req, "weight", 0f);
			float bust=ServletRequestUtils.getFloatParameter(req, "bust", 0f);
			float waistline=ServletRequestUtils.getFloatParameter(req, "waistline", 0f);
			float hips=ServletRequestUtils.getFloatParameter(req, "hips", 0f);

			pwd = SecurityUtil.encoderByMd5(pwd);
			Date birth = "".equals(birthday) ? null : sdf.parse(birthday);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);

			service.add(roleId, account, pwd, name, sex, birth, phone, email, idcard, height,  weight,  bust,  waistline,  hips, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 编辑资料
	 * 
	 * @return
	 */
	public String edit() {
		try {
			HttpServletRequest req = getRequest();
			String userId = ServletRequestUtils.getStringParameter(req, "userId");
			String roleId = ServletRequestUtils.getStringParameter(req, "roleId");
			String name = ServletRequestUtils.getStringParameter(req, "name", "");
			int sex = ServletRequestUtils.getIntParameter(req, "sex", 1);
			String birthday = ServletRequestUtils.getStringParameter(req, "birthday", "");
			String phone = ServletRequestUtils.getStringParameter(req, "phone", "");
			String email = ServletRequestUtils.getStringParameter(req, "email", "");
			String idcard = ServletRequestUtils.getStringParameter(req, "idcard", "");
			float height=ServletRequestUtils.getFloatParameter(req, "height", 0f);
			float weight=ServletRequestUtils.getFloatParameter(req, "weight", 0f);
			float bust=ServletRequestUtils.getFloatParameter(req, "bust", 0f);
			float waistline=ServletRequestUtils.getFloatParameter(req, "waistline", 0f);
			float hips=ServletRequestUtils.getFloatParameter(req, "hips", 0f);

			Date birth = "".equals(birthday) ? null : sdf.parse(birthday);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);

			service.edit(userId, roleId, name, sex, birth, phone, email, idcard, height,  weight,  bust,  waistline,  hips, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 管理员重置密码
	 * 
	 * @return
	 */
	public String resetPwd() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String userId = ServletRequestUtils.getStringParameter(req, "userId");
			String pwd = ServletRequestUtils.getStringParameter(req, "pwd");

			pwd = SecurityUtil.encoderByMd5(pwd);
			service.resetPwd(userId, pwd);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除记录
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");

			service.remove(cbs);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 检测无此账号
	 * 
	 * @return
	 */
	public String checkNoAccount() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String account = ServletRequestUtils.getStringParameter(req, "account");
			UserVO vo = service.getUser(account);
			if (null == vo) {
				map.put("code", CodeUtil.SUCCESS);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 编辑个人密码
	 * 
	 * @return
	 */
	public String personEdit() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String name = ServletRequestUtils.getStringParameter(req, "name", "");
			int sex = ServletRequestUtils.getIntParameter(req, "sex", 1);
			String birthday = ServletRequestUtils.getStringParameter(req, "birthday", "");
			String phone = ServletRequestUtils.getStringParameter(req, "phone", "");
			String email = ServletRequestUtils.getStringParameter(req, "email", "");
			String idcard = ServletRequestUtils.getStringParameter(req, "idcard", "");
			float height=ServletRequestUtils.getFloatParameter(req, "height", 0f);
			float weight=ServletRequestUtils.getFloatParameter(req, "weight", 0f);
			float bust=ServletRequestUtils.getFloatParameter(req, "bust", 0f);
			float waistline=ServletRequestUtils.getFloatParameter(req, "waistline", 0f);
			float hips=ServletRequestUtils.getFloatParameter(req, "hips", 0f);

			Date birth = "".equals(birthday) ? null : sdf.parse(birthday);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);

			service.personEdit(userVO.getUserId(), name, sex, birth, phone, email, idcard, height,  weight,  bust,  waistline,  hips, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 修改个人密码
	 * 
	 * @return
	 */
	public String personResetPwd() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String oldPwd = ServletRequestUtils.getStringParameter(req, "oldPwd");
			String newPwd = ServletRequestUtils.getStringParameter(req, "newPwd");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			oldPwd = SecurityUtil.encoderByMd5(oldPwd);
			newPwd = SecurityUtil.encoderByMd5(newPwd);
			if (service.personResetPwd(userVO.getUserId(), oldPwd, newPwd)) {
				map.put("code", CodeUtil.SUCCESS);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

}
